package com.fxz.oauth.security.userdetails;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fxz.oauth.domain.User;
import com.fxz.oauth.service.impl.UserServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.stereotype.Service;

/**
 * 可以进行用户密码升级
 *
 * @author Fxz
 * @version 1.0
 * @date 2021-08-04 10:56
 */
@Service
@AllArgsConstructor
public class UserDetailsPasswordServiceImpl implements UserDetailsPasswordService {

    private final UserServiceImpl userService;

    /**
     * 根据用户名查询出用户 同时对用户密码进行编码升级
     *
     * @param userDetails 用户信息
     * @param s 进行最新编码后的密码
     * @return
     */
    @Override
    public UserDetails updatePassword(UserDetails userDetails, String s) {
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, userDetails.getUsername()));
        if (ObjectUtils.isNotEmpty(user)) {
            user.setPassword(s);
            userService.updateById(user);
        }
        return user;
    }
}
